package iit.projects.etaxi.view.bean;

import iit.projects.etaxi.model.service.ServiceLocator;
import iit.projects.etaxi.util.Constants;
import iit.projects.etaxi.util.JSFUtils;

import java.util.Iterator;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NavigationBackingBean {
	
	private static final Log log = LogFactory.getLog(NavigationBackingBean.class);
	
	private boolean messagePopupVisible;
	private String messagePopupTitle;
	private String messagePopupStyleClass;
	
	@SuppressWarnings("unused")
	private ServiceLocator serviceLocator;
	
	@javax.annotation.PostConstruct
	public void init(){
		//JSFUtils.storeOnSession("loggedIn", Boolean.FALSE);
	}
	
	public boolean isMessagePopupVisible(){

	    messagePopupVisible = false;
	    Iterator<FacesMessage> messageIterator = FacesContext.getCurrentInstance().getMessages(null);
		messagePopupVisible = messageIterator.hasNext();
		if (messagePopupVisible) {
			Object requestValue = JSFUtils.getFromSession("popupVisible");
			boolean popupVisible = (requestValue != null) ? (Boolean) requestValue : false;
			if (log.isDebugEnabled()) {
				log.debug("Popup Visible: " + popupVisible);
			}
			if (popupVisible) {
				messagePopupVisible = false;
			} else {
				FacesMessage message = messageIterator.next();
				int ordinal = message.getSeverity().getOrdinal();
				if (messageIterator.hasNext()) {
					messagePopupTitle = Constants.FacesMessage.MESSAGE_POPUP_TITLE_MULTIPLE;
					do {
						message = messageIterator.next();
						if (ordinal < message.getSeverity().getOrdinal()) {
							ordinal = message.getSeverity().getOrdinal();
						}
					} while (messageIterator.hasNext());
				} else {
					if (FacesMessage.SEVERITY_INFO.getOrdinal() == message.getSeverity().getOrdinal()) {
						messagePopupTitle = Constants.FacesMessage.MESSAGE_POPUP_TITLE_INFO;
					} else if (FacesMessage.SEVERITY_WARN.getOrdinal() == message.getSeverity().getOrdinal()) {
						messagePopupTitle = Constants.FacesMessage.MESSAGE_POPUP_TITLE_WARN;
					} else if (FacesMessage.SEVERITY_ERROR.getOrdinal() == message.getSeverity().getOrdinal()) {
						messagePopupTitle = Constants.FacesMessage.MESSAGE_POPUP_TITLE_ERROR;
					} else if (FacesMessage.SEVERITY_FATAL.getOrdinal() == message.getSeverity().getOrdinal()) {
						messagePopupTitle = Constants.FacesMessage.MESSAGE_POPUP_TITLE_FATAL;
					} else {
						messagePopupTitle = Constants.FacesMessage.MESSAGE_POPUP_TITLE_SINGLE;
					}
				}
				
				if (FacesMessage.SEVERITY_FATAL.getOrdinal() == ordinal) {
					messagePopupStyleClass = Constants.FacesMessage.MESSAGE_STYLE_CLASS_FATAL;
				} else if (FacesMessage.SEVERITY_ERROR.getOrdinal() == ordinal) {
					messagePopupStyleClass = Constants.FacesMessage.MESSAGE_STYLE_CLASS_ERROR;
				} else if (FacesMessage.SEVERITY_WARN.getOrdinal() == ordinal) {
					messagePopupStyleClass = Constants.FacesMessage.MESSAGE_STYLE_CLASS_WARN;
				} else {
					messagePopupStyleClass = Constants.FacesMessage.MESSAGE_STYLE_CLASS_INFO;
				}

				//javaScriptCallHideScrollbarsOnly();
				//javaScriptCallSetFormFocus("headerForm:btnPopupMessageOK");
			}
		}
		return messagePopupVisible;
	}
	
	public void login(ActionEvent event){
		JSFUtils.storeOnSession("loggedIn", Boolean.TRUE);
	}
	
	public String logOut(){
		JSFUtils.storeOnSession("loggedIn", Boolean.FALSE);
		
		FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = ctx.getExternalContext();
//        try {
//			ectx.redirect("../../../index.iface");
//		} catch (IOException e) {
//			log.error("Error" , e);
//		}
       // HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
        HttpSession session = (HttpSession)ectx.getSession(false);
        session.invalidate();
        ctx.responseComplete();  
        return "home";
        //response.sendRedirect("index.iface");
	}
	
	public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
	
	public void closeMessagePopup(ActionEvent event){
		messagePopupVisible = false;
	}
	
	public void setMessagePopupVisible(boolean messagePopupVisible) {
		this.messagePopupVisible = messagePopupVisible;
	}

	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	public String getMessagePopupTitle() {
		return messagePopupTitle;
	}

	public String getMessagePopupStyleClass() {
		return messagePopupStyleClass;
	}
}
